<?php

header("Content-type:text/html;charset=utf-8");


/**
 * 把json信息组织信息名称转换为字符串
 * @param $json
 * @return string
 */
function convertClassJson($json)
{
    $classarr = json_decode($json, true);
    $classarr = array_reverse($classarr);

    $class = '';
    foreach ($classarr as $c){
        $class = $c.', '.$class;
    }
    return $class;
}

function convertToQuizId($quiz_name){
   $rs =  M("quiz_info")->where(['quiz_name' => $quiz_name] )->find();
    return $rs['quiz_id'];
}

function convertQuizListJson($json){
    $quiz_list_arr = json_decode($json, true);
    $str = "";
    foreach($quiz_list_arr as $quiz_name){
        $quiz_info = M('quiz_info')->where(['quiz_name' => $quiz_name])->select();
        $quiz_title = $quiz_info[0]['quiz_pagetitle'];

        $str = $quiz_title.", ".$str;
    }

    return $str;

}

function I_withoutReplace($para)
{
    $str = I($para);
    return str_replace("&quot;", "\"",$str);
}

function convertUnixTime($unixtime){
    return date('Y-m-d H:i:s', $unixtime);
}

function convertSex($str){
    if ($str == 'female')
        return '女';
    else
        return '男';
}

function http($url, $params, $method = 'GET', $header = array(), $multi = false){
    $opts = array(
        CURLOPT_TIMEOUT        => 30,
        CURLOPT_RETURNTRANSFER => 1,
        CURLOPT_SSL_VERIFYPEER => false,
        CURLOPT_SSL_VERIFYHOST => false,
        CURLOPT_HTTPHEADER     => $header
    );
    /* 根据请求类型设置特定参数 */
    switch(strtoupper($method)){
        case 'GET':
            $opts[CURLOPT_URL] = $url . '?' . http_build_query($params);
            break;
        case 'POST':
            //判断是否传输文件
            $params = $multi ? $params : http_build_query($params);
            $opts[CURLOPT_URL] = $url;
            $opts[CURLOPT_POST] = 1;
            $opts[CURLOPT_POSTFIELDS] = $params;
            break;
        default:
            throw new Exception('不支持的请求方式！');
    }
    /* 初始化并执行curl请求 */
    $ch = curl_init();
    curl_setopt_array($ch, $opts);
    $data  = curl_exec($ch);
    $error = curl_error($ch);
    curl_close($ch);
    if($error) throw new Exception('请求发生错误：' . $error);
    return  $data;
}
